<?php


	$a = $_GET['a'];
	$id = $_GET['id'];
	//dis功能里面的header跳转前不能有输出，故再此做个区分
	if($a == 'add' || $a == 'edit'){
	include '../admin_head.php';
	}else{
	include '../common.php';
	}


		switch($a){
			case 'add':
				$cate_id = $_POST['pid'];	
				$name = trim($_POST['name']);	
				$price = trim($_POST['price']);	
				$store = trim($_POST['store']);
				$status = $_POST['status']? array_sum($_POST['status']):0 ;	
				$style = $_POST['style'];
				$is_up = $_POST['is_up'];	
				$describe = $_POST['describe'];
				$addtime = time();

				//判断填写信息完整性
				if(empty($name) || empty($price) || empty($store) || empty($describe)) exit(admin_jump('信息填写不完整', 3, './add.php'));
				//上传图片
				if($_FILES['pic']['error'] == 4){
                exit(admin_jump('请上传商品图片', 3, './add.php'));
				}

				if(!$filename = upload('pic',PATH.'/../upload/')) 
					exit(admin_jump('图片上传失败','3','./add.php'));

				//缩放图片
				if(!zoom(PATH.'/../upload/',$filename,235,317)){
					@unlink(PATH.'/../upload/'.$filename);
					exit(admin_jump('图片缩放失败','3','./add.php'));
				}
					//删除上传后的原尺寸的图片（没用）
					@unlink(PATH.'/../upload/'.$filename);

				//写入数据库
				$image = '317_'.$filename;
				$sql = "insert into ".PRE."goods(cate_id,name,price,image,store,status,style,is_up,`describe`,addtime)  values('{$cate_id}','{$name}','{$price}','{$image}','{$store}','{$status}','{$style}','{$is_up}','{$describe}','{$addtime}')";
				$result = mysql_query($sql);
				if($result && mysql_insert_id($link)>0){
				   	echo admin_jump('上传成功','3','./add.php');
				}else{
					@unlink(PATH.'/../upload/'.$image);
				   	echo admin_jump('上传失败','3','./add.php');
				}
				break;

			case 'dis':
				$dis = $_GET['dis'];
				$sql = "update ".PRE."goods set is_up='{$dis}' where id='{$id}'";
				$result = mysql_query($sql);
				header('Location:'.$_SERVER['HTTP_REFERER']);	
				break;

			case 'status':
				$status = $_GET['status'];
				$sql = "update ".PRE."goods set status='{$status}' where id='{$id}'";
				$result = mysql_query($sql);
				header('Location:'.$_SERVER['HTTP_REFERER']);
				break;

			case 'edit':
				$page = $_GET['p'];
				$category = $_GET['cg'];
				$style = $_GET['st'];
				$id = $_GET['id'];
				$cate_id = $_POST['pid'];
				$name = $_POST['name'];
				$price = $_POST['price'];
				$store = $_POST['store'];
				$style = $_POST['style'];
				$describe = $_POST['describe'];
				if($_FILES['pic']['error'] == 4){
				//没有更新图片
					$sql = "update ".PRE."goods set cate_id='{$cate_id}',name='{$name}',price='{$price}',store='{$store}',style='{$style}',`describe`='{$describe}' where id='{$id}'";
					$result = mysql_query($sql);
					if($result && mysql_affected_rows($link) > 0){
						echo admin_jump('更新成功',2,'index.php?cg='.$category.'&st='.$style.'&p='.$page);
					}else{
						echo admin_jump('更新失败',2,$_SERVER['HTTP_REFERER']);
					}
				}else{
				//更新图片
					$sql = "select image from ".PRE."goods where id='{$id}'";
					$oldname = mysql_fetch_assoc(mysql_query($sql));
					$oldname = $oldname['image'];
					if(!$filename = upload('pic',PATH.'/../upload/')) exit(admin_jump('图片上传失败',2,$_SERVER['HTTP_REFERER']));
					if(!zoom(PATH.'/../upload/',$filename,235,317)){
					unlink(PATH.'/../upload/'.$filename);
					exit(admin_jump('图片缩放失败','3','./add.php'));
					}
					//删除上传后的原尺寸的图片（没用）
					@unlink(PATH.'/../upload/'.$filename);
					//删除之前老图片
					@unlink(PATH.'/../upload/'.$oldname);
					//写入数据库
					$image = '317_'.$filename;
					$sql = "update ".PRE."goods set cate_id='{$cate_id}',name='{$name}',price='{$price}',store='{$store}',style='{$style}',`describe`='{$describe}',image='{$image}' where id='{$id}'";
					$result = mysql_query($sql);
					if($result && mysql_affected_rows($link) > 0){
						echo admin_jump('更新成功',2,'index.php?cg='.$category.'&st='.$style.'&p='.$page);
					}else{
						echo admin_jump('更新失败',2,$_SERVER['HTTP_REFERER']);
					}

				}

				break;
				
			case 'addimg':
				if(!$names = multi_upload('pic' ,PATH.'/../upload/'))  exit(admin_jump('图片上传失败',2,$_SERVER['HTTP_REFERER']));
					foreach($names as $name){
						//详情图不缩放  调用图片的时候，设置img宽度
						if(!simzoom(PATH.'/../upload/',$name,780)){
							@unlink(PATH.'/../upload/'.$name);
							header('Location:'.$_SERVER['HTTP_REFERER']);
						}
						//删除上传后的原尺寸的图片（没用）
						@unlink(PATH.'/../upload/'.$name); 
						

						//写入数据库
						$name = '780_'.$name;
						$sql = "insert into ".PRE."image(name,goods_id) values('{$name}','{$id}')";
						mysql_query($sql);
					}
					
					header('Location:'.$_SERVER['HTTP_REFERER']);
				
				break;
				
			case 'delimg':
				$name = $_GET['name'];
				@unlink(PATH.'/../upload/'.$name); 
				$sql = "delete from ".PRE."image where id='{$id}'";
				mysql_query($sql);
				header('Location:'.$_SERVER['HTTP_REFERER']);
				
				break;
		}	
	
include '../admin_footer.php';
?>
